//
//  KXChannelVC.swift
//  YunshuApp
//
//  Created by 周文杰 on 2017/6/2.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit

typealias func1 = (String)->Void

class KXChannelVC: UIViewController {

    let presenter = KXChannelsPresenter.init()
    
    var onComplete:func1?
    var singleSelect = false
    var selected:[String] = []
    
    init() {
       super.init(nibName: nil, bundle: nil)
        self.title = "选择类别"
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addSubview(presenter.view)
        presenter.relayout()
        
        presenter.view.frame = CGRect.init(x: 0, y: 64, width: Const.SCREEN_WIDTH, height: Const.SCREEN_HEIGHT - 64 - 68)
        presenter.singleSelect = singleSelect
//        self.presenter.selected = self.selected.map({ cid in
//            let channel = KXChannel.init()
//            channel.channelId = cid
//            channel.title = cid
//            return channel
//        })
        
        let bottom = UIView.init(frame: CGRect.init(x: 0, y: Const.SCREEN_HEIGHT - 68, width: Const.SCREEN_WIDTH, height: 68))
        bottom.backgroundColor = Const.WTM_WHITE_SMOKE
        self.view.addSubview(bottom)
        let button = UIButton.init(type: .roundedRect)
        button.backgroundColor = Const.WTM_GREEN
        button.setTitleColor(UIColor.white, for: .normal)
        button.addTarget(self, action: #selector(KXChannelVC.onTapConfirm), for: .touchUpInside)
        button.setTitle("确定", for: .normal)
        button.frame = CGRect.init(x: 10, y: 10, width: Const.SCREEN_WIDTH - 20 , height: 48)
        bottom.addSubview(button)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    @objc func onTapConfirm() {
        
        let result = self.presenter.selected
        var result2 = result.reduce("") { (result, channel) -> String in
            result + channel.title + ","
        }
        if(result2.count > 0){
            result2 = result2.substring(to: result2.index(before: result2.endIndex))
        }
        if let function = self.onComplete  {
            function(result2)
        }
        self.navigationController?.popViewController(animated: true)
    }
}
